package com.siao.example.loading.controller
{
	import com.siao.core.Log;
	import com.siao.data.XmlValue;
	import com.siao.example.loading.method.LoadMethod;
	import com.siao.example.loading.model.AssetsLoadModel;
	import com.siao.example.loading.state.LoadingState;
	import com.siao.mvc.Controller;
	import com.siao.mvc.core.INotice;
	import com.siao.utils.math.round;
	
	/**
	 * 资源加载控制器
	 * @author SiaoLeon
	 */
	public class AssetsLoadController extends Controller
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function AssetsLoadController()
		{
			super();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		override public function onAdd():void
		{
			this.register(LoadMethod.LOAD_ASSETS, LOAD_ASSETS);
			this.register(LoadMethod.LOAD_PROGRESS, LOAD_PROGRESS);
			this.register(LoadMethod.LOAD_ERROR, LOAD_ERROR);
		}
		
		override public function onRemove():void
		{
		
		}
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		private function LOAD_ASSETS(notice:INotice):void
		{
			(this.context.getModel(AssetsLoadModel) as AssetsLoadModel).load(notice.params);
		}
		
		private function LOAD_PROGRESS(notice:INotice):void
		{
			var model:AssetsLoadModel = this.context.getModel(AssetsLoadModel) as AssetsLoadModel;
			(this.context as LoadingState).view.setProgress(round(model.currentLoaded / model.currentTotal / model.countTotal + model.countLoaded / model.countTotal, 2));
		}
		
		private function LOAD_ERROR(notice:INotice):void
		{
			var asset:XmlValue = notice.params;
			Log.error("加载资源" + asset.name + "失败，地址：" + asset.value);
		}
	
	}
}
